﻿//var url = "https://sweltering-torch-7921.firebaseio.com/";
//var firebaseRef = new Firebase(url);

var shipsArray = [];

var ship = {
    name: "Nick's ship",
    type: 'B-Wing',
    Id: null
}

var xhr = new XMLHttpRequest();
xhr.open("PUT", "https://sweltering-torch-7921.firebaseio.com/-JX-ORZA4Iqc8RVyjrzZ.json", true);
xhr.onload = function () {
    if (this.status >= 200 && this.status < 400) {
        //This is the ID (Firebase calls it name) of the object when you do a POST (create)
        //ship.Id = JSON.parse(this.response).name;
        //shipsArray.push(ship);
        document.getElementById('result').innerHTML = "ship added successfully";
    }
};
xhr.onerror = function () {
    alert('oh no something happened');
};

var showMyShips = function () {
    var xhr2 = new XMLHttpRequest();
    xhr2.open("GET", "https://sweltering-torch-7921.firebaseio.com/.json", true);
    xhr2.onload = function () {
        if (this.status >= 200 && this.status < 400) {
            var text = "";
            var ships = JSON.parse(this.response);
            for (var s in ships) {
                ships[s].Id = s;
                shipsArray.push(ships[s]);
            }
            document.getElementById('shipsResult').innerHTML = text;

            shipsArray[2].name = "Dexter";
            xhr.send(JSON.stringify(shipsArray[2]));
        }
    };
    xhr2.onerror = function () {
        alert('oh no something happened');
    };
    xhr2.send();
}
